package com.gdth.database.bean;

import java.util.List;

import com.gdth.bank.BankApplication;

import android.content.Context;

public class DbService {
	private static DbService instance;
	private static Context appContext;
	private DaoSession mDaoSession;
	private NoteDao noteDao;

	public static DbService getInstance() {
		if (instance == null) {
			instance = new DbService();
			if (appContext == null){
				appContext = BankApplication.mInstance.getApplicationContext();
			}
			instance.mDaoSession = BankApplication.getDaoSession();
//			instance.noteDao = instance.mDaoSession.getNoteDao();
		}
		return instance;
	}

	public Note loadNote(long id) {
		return noteDao.load(id);
	}

	public List<Note> loadAllNote(){
		return noteDao.loadAll();
	}

	public List<Note> queryNote(String where, String... params){
		return noteDao.queryRaw(where, params);
	}

	public long saveNote(Note note){
		return noteDao.insertOrReplace(note);
	}

	public void saveNoteLists(final List<Note> list){
		if(list == null || list.isEmpty()){
			return;
		}
		noteDao.getSession().runInTx(new Runnable() {
			@Override
			public void run() {
				for(int i=0; i<list.size(); i++){
					Note note = list.get(i);
					noteDao.insertOrReplace(note);
				}
			}
		});

	}

	public void deleteAllNote(){
		noteDao.deleteAll();
	}

	public void deleteNote(long id){
		noteDao.deleteByKey(id);
	}

	public void deleteNote(Note note){
		noteDao.delete(note);
	}
}
